﻿Imports Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Imports Enumerable = System.Linq.Enumerable

Public Class GetSMSProvideData
    Public Shared Sub Getcomsys(ByVal connection As WqlConnectionManager, ByVal queryString As String, ByVal RDP As Boolean)
        Dim resId As String = Nothing
        Dim userName As String = Nothing
        Dim model As String = Nothing
        Dim dn As String = Nothing
        Dim serialNum As String = Nothing
        Dim ipAddress As String = Nothing
        Dim macAddress As String = Nothing
        Dim comname As String = Nothing
        Dim strbackupusrname As String = Nothing
        Dim manufact As String = Nothing
        Dim strSysOU As String = Nothing
        Dim stros As String = Nothing
        Dim objrsys = connection.QueryProcessor.ExecuteQuery("select * from SMS_R_System full join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.resourceID = SMS_R_System.resourceID full join SMS_G_System_PC_BIOS on SMS_G_System_PC_BIOS.resourceid = SMS_R_System.resourceID full join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID = SMS_R_System.resourceID where " & queryString & "")
        Dim comdetails = New List(Of String)
        For Each res In objrsys
            Dim smssys As Object
            smssys = res.properties("SMS_R_System").objectvalue
            resId = DirectCast(smssys.Properties("ResourceID"), Management.PropertyData).Value
            ipAddress = Enumerable.Cast(Of Object)(DirectCast(smssys.Properties("IPAddresses"), Management.PropertyData).Value).Aggregate(ipAddress, Function(current, resip) current & "," & resip.ToString())
            macAddress = Enumerable.Cast(Of Object)(DirectCast(smssys.Properties("MACAddresses"), Management.PropertyData).Value).Aggregate(macAddress, Function(current, resip) current & "," & resip.ToString())
            comname = DirectCast(smssys.Properties("Name"), Management.PropertyData).Value
            If Not (DirectCast(smssys.Properties("LastLogonUserDomain"), Management.PropertyData).Value = Nothing And DirectCast(smssys.Properties("LastLogonUserName"), Management.PropertyData).Value = Nothing) Then
                userName = DirectCast(smssys.Properties("LastLogonUserDomain"), Management.PropertyData).Value & "\" & DirectCast(smssys.Properties("LastLogonUserName"), Management.PropertyData).Value
            End If
            Try
                strSysOU = DirectCast(DirectCast(smssys.Properties("SystemOUName"), Management.PropertyData).Value, String())(DirectCast(DirectCast(smssys.Properties("SystemOUName"), Management.PropertyData).Value, String()).Length - 1)
                If strSysOU = Nothing Then strSysOU = DirectCast(DirectCast(smssys.Properties("SystemContainerName"), Management.PropertyData).Value, String())(DirectCast(DirectCast(smssys.Properties("SystemContainerName"), Management.PropertyData).Value, String()).Length - 1)
            Catch ex As Exception
                strSysOU = DirectCast(DirectCast(smssys.Properties("SystemContainerName"), Management.PropertyData).Value, String())(DirectCast(DirectCast(smssys.Properties("SystemContainerName"), Management.PropertyData).Value, String()).Length - 1)
            End Try

            Dim comsys As Object
            comsys = res.properties("SMS_G_System_COMPUTER_SYSTEM").objectvalue
            strbackupusrname = DirectCast(comsys.Properties("UserName"), Management.PropertyData).Value
            model = DirectCast(comsys.Properties("Model"), Management.PropertyData).Value
            manufact = DirectCast(comsys.Properties("Manufacturer"), Management.PropertyData).Value
            Dim pcbios As Object
            pcbios = res.properties("SMS_G_System_PC_BIOS").objectvalue
            serialNum = DirectCast(pcbios.Properties("SerialNumber"), Management.PropertyData).Value
            Dim os As Object
            os = res.properties("SMS_G_System_OPERATING_SYSTEM").objectvalue
            stros = DirectCast(os.Properties("caption"), Management.PropertyData).Value
            If DirectCast(comsys.Properties("SystemType"), Management.PropertyData).Value = "x64-based PC" Then stros = stros & " x64" Else stros = stros & " x86"
        Next
        If Not macAddress = Nothing Then
            ' ReSharper disable VBWarnings::BC42104
            If macAddress.StartsWith(",") Then
                macAddress = macAddress.Substring(1, macAddress.Length - 1)
            End If
        End If
        If Not ipAddress = Nothing Then
            ' ReSharper disable VBWarnings::BC42104
            If ipAddress.Contains(":") Then
                Dim ip = Split(ipAddress, ",")
                ipAddress = (From ipadd In ip Where Not ipadd.Contains(":")).Aggregate(Of String)(Nothing, Function(current, ipadd) current & "," & ipadd)
            End If
            If ipAddress.StartsWith(",,") Then
                ipAddress = ipAddress.Substring(2, ipAddress.Length - 2)
                ' ReSharper restore VBWarnings::BC42104
                'ipAddress = ip(1).ToString()
            End If
            If ipAddress.Contains(",") Then ipAddress = ipAddress.Replace(",", "  ")
            If macAddress.Contains(",") Then macAddress = macAddress.Replace(",", "  ")
        End If
        If userName = Nothing Then
            userName = strbackupusrname
        End If
        comdetails.Add(userName & "|" & comname & "|" & serialNum & "|" & model & "|" & macAddress & "|" & resId & "|" & dn & "|" & ipAddress & "|" & manufact & "|" & strSysOU & "|" & stros)
        FillForm.FillFormboxes(comdetails, "comp", RDP)
    End Sub
End Class
